﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;

namespace LogisticsAndong
{
    public class PublicHelper
    {
        /// <summary>
        /// PING IP地址
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool PingIp(string ip)
        {
            bool result = false;
            try
            {
                Ping pingSend = new Ping();
                PingReply reply = pingSend.Send(ip, 2000);
                if (reply.Status == IPStatus.Success)
                    result = true;
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        /// <summary>
        /// 字节数组转二进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string ByteToStr(byte[] bytes)
        {
            string strResult = "";
            for (int i = 0; i < bytes.Length; i++)
            {
                string strTemp = Convert.ToString(bytes[i], 2);
                strTemp = strTemp.Insert(0, new string('0', 8 - strTemp.Length));
                //倒叙
                strTemp = ReverseD(strTemp);
                strResult += strTemp;
            }
            return strResult;
        }

        /// <summary>
        /// 二进制字符串转字节数组
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StrToByte(string str)
        {
            byte[] bytes = new byte[str.Length / 8];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = Convert.ToByte(str.Substring(i * 8, 8), 2);
            }
            return bytes;
        }

        /// <summary>
        /// 字符串倒叙
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string ReverseD(string text)
        {
            return new string(text.ToCharArray().Reverse().ToArray());
        }
    }
}
